//
//  StepThreeViewController.m
//  nonghubao
//
//  Created by Fenrir on 13-8-17.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "StepThreeViewController.h"
#import "Helper.h"
#import "Config.h"
#import "StepThreeViewController.h"
#import "UICustomTextField.h"
#import "JMFBase.h"

@interface StepThreeViewController ()
{
    UICustomTextField *_newPWDTextField;
    UICustomTextField *_comfirmPWDTextField;
}
@end

@implementation StepThreeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = _L(@"vc_title_072");
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_012@2x" ofType:@"png"]]];
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 85.0f, 20.0f)];
    titleLabel.text = _L(@"title_84");
    titleLabel.font = _SYS_FONT_003;
    
    _newPWDTextField = [[UICustomTextField alloc] initWithFrame:CGRectMake(10.0f, 11.0f, 300.0f, 45.0f)];
    _newPWDTextField.background = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_010@2x" ofType:@"png"]];
    _newPWDTextField.placeholder = _L(@"holder_36");
    _newPWDTextField.delegate = self;
    _newPWDTextField.secureTextEntry = YES;
    _newPWDTextField.returnKeyType = UIReturnKeyDone;
    _newPWDTextField.leftView = titleLabel;
    _newPWDTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _newPWDTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_newPWDTextField];
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 85.0f, 20.0f)];
    titleLabel.text = _L(@"title_85");
    titleLabel.font = _SYS_FONT_003;
    
    _comfirmPWDTextField = [[UICustomTextField alloc] initWithFrame:CGRectMake(10.0f, _newPWDTextField.frame.origin.y+_newPWDTextField.frame.size.height+10.0f, 300.0f, 45.0f)];
    _comfirmPWDTextField.background = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_010@2x" ofType:@"png"]];
    _comfirmPWDTextField.returnKeyType = UIReturnKeyDone;
    _comfirmPWDTextField.placeholder = _L(@"holder_37");
    _comfirmPWDTextField.secureTextEntry = YES;
    _comfirmPWDTextField.delegate = self;
    _comfirmPWDTextField.leftView = titleLabel;
    _comfirmPWDTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _comfirmPWDTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_comfirmPWDTextField];
    
    UIButton *bottomButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, _SYS_FRAME_HEIGHT-94.0f, 300.0f, 44.0f)];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomButton setTitle:_L(@"btn_yes_31") forState:UIControlStateNormal];
    [bottomButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_btn_006_n@2x" ofType:@"png"]] forState:UIControlStateNormal];
    bottomButton.titleLabel.font = _SYS_FONT_007;
    [bottomButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)buttonEvent:(id)sender
{
    JSONRequest *request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ACCOUNT_TRANSFER_FORGOT_SECURITYPWD_THIRD];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_01");
    [request setValue:_newPWDTextField.text forParameter:@"securityPwd"];
    [request setValue:_comfirmPWDTextField.text forParameter:@"confirmPwd"];
    
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"processId"] != 0) {
        
        [request setValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"processId"] forParameter:@"transferAccountId"];
    }
    [request asynchronous];
}

#pragma mark - JSONRequestDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        [Helper showAllTextDialog:jmf.message inSeconds:2.0f];
        
        //设置找回安全密码已完成
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"processCompleted"];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        //reset 处理中的转账信息id
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"processId"];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
    
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

@end
